home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Night Owl 6
/
Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso
/
016a
/
frbts_20.zip
/
FIDONET.C
next >
Wrap
C/C++ Source or Header
|
1991-04-28
|
19KB
|
622 lines
/****************************************************************************/
/* */
/* File.c Version 2.0 By Craig Derouen */
/* File i/o processsing module for Frobot.c */
/* */
/* */
/****************************************************************************/
#include "frobot.h"
#include <share.h>
#include <time.h>
#include "globals.h"
#define MAXFMSGS 999 /* Maximum # of Fido messages to scan for */
/* Process a fidonet filesend function */
void file_send(char *p1,char *line,char *sendfile)
{
FILE *flfile;
FILE *incfile;
int code,results,mode;
mode = 0; /* If mode == 1, do file delete after sending
if mode == 2, do file truncate after sending */
/* Command could be "SD" for delete or "ST" for truncate */
if (tolower(*(p1+1)) == 'd')
mode = 1;
else if (tolower(*(p1+1)) == 't') mode = 2;
if ((flfile = _fsopen(FloFile,"at+",SH_DENYWR)) == NULL) {
sprintf(temp,"Error opening/creating file %s",FloFile);
aborterror(BADFILE,temp);
}
if (*sendfile == '@') { /* Is it an include file? */
if ((incfile = _fsopen(sendfile+1,"rt",SH_DENYNO)) == NULL) {
sprintf(temp,"Error opening file %s",sendfile+1);
aborterror(BADFILE,temp);
}
results = 0;
while (fgets(temp,65,incfile)) {
if (*temp != ';') { /* Commented out */
temp[strlen(temp)-1] = 0; /* Strip out line feed */
if (test_sfile(temp,flfile,mode) == FALSE) { /* A file was sent */
DoFSendMsg(temp);
results++;
}
}
}
if (results) { /* We sent some files */
sprintf(temp," %d File(s) sent on %s",results,datestr);
commentline(line,temp);
FileSends += results;
}
fclose(incfile);
}
else { /* It's an individal file */
code = test_sfile(sendfile,flfile,mode);
switch (code) {
case -1: /* File already in flow file */
commentline(line,"File(s) are already being sent. Disabled");
break;
case FALSE: /* File is sent */
DoFSendMsg(sendfile);
sprintf(temp,"File(s) sent on %s",datestr);
commentline(line,temp);
FileSends++;
break;
case TRUE: /* File not found */
commentline(line,"File(s) to send were not found!");
break;
}
}
fclose(flfile);
}
/* Process a fidonet filerequest function */
void file_request(char *line,char *requestfile)
{
FILE *flfile;
FILE *incfile;
FILE *incbfile;
char backup[_MAX_PATH];
char *p,*fname;
int filetimes = 0,results;
int retries = 0;
int files = -1;
int doreqs = 0;
int reqcreate;
if(stat(ReqFile,&fbuf) == -1)
reqcreate = TRUE;
else reqcreate = FALSE;
if ((flfile = _fsopen(ReqFile,"at+",SH_DENYWR)) == NULL) {
sprintf(temp,"Error opening/creating file %s",ReqFile);
aborterror(BADFILE,temp);
}
if (*requestfile == '@') { /* Is it an include file? */
if ((incfile = _fsopen(requestfile+1,"rt",SH_DENYNO)) == NULL) {
sprintf(temp,"Error opening file %s",requestfile+1);
aborterror(BADFILE,temp);
}
createbackup(requestfile+1,backup);
if ((incbfile = _fsopen(backup,"wt+",SH_DENYNO)) == NULL) {
sprintf(temp,"Error creating temp file %s",backup);
aborterror(BADFILE,temp);
}
while (fgets(temp,65,incfile)) {
blanktrim(temp);
if (!*temp) /* It's a blank line just delete it */
continue;
if (*temp != ';') {
if (files == -1) /* set a flag */
files = 0;
files++;
strcpy(temp1,temp);
fname = strtok(temp1," \t\n"); /* File name */
p = strtok(NULL," =\t\n"); /* Count */
if (p) {
filetimes = atoi(p);
p = strtok(NULL," \t\n");
if (p) retries = atoi(p);
else retries = 0;
}
else {
filetimes = FileCount; /* Plug in default */
retries = 0;
}
p = strtok(fname,"!");
strcpy(password,strtok(NULL," \n"));
results = test_rfile(p,flfile,filetimes,retries);
switch (results) {
case -1: /* File received already */
sprintf(temp,";%s %d=%d <- File received on %s",
fname,filetimes,retries,datestr);
DoNoteMsg(fname);
break;
case -2: /* File already exists in reqfile */
break;
case -3: /* Maximum retries */
break;
case -4: /* Trashed name */
sprintf(temp,";%s %d <- Filename has trashed/illegal characters",fname,filetimes);
break;
case 1: /* File is requested, disabled because magicfilename */
doreqs++;
files--;
if (password[0])
sprintf(temp,";%s!%s %d=1 <- MagicFilename retries disabled",fname,password,filetimes);
else sprintf(temp,";%s %d=1 <- MagicFilename retries disabled",fname,filetimes);
DoFReqMsg(fname);
break;
case 2: /* File requested, normal */
doreqs++;
retries++;
files--;
if (password[0])
sprintf(temp,"%s!%s %d=%d ",fname,password,filetimes,retries);
else sprintf(temp,"%s %d=%d ",fname,filetimes,retries);
DoFReqMsg(fname);
break;
}
}
strcat(temp,"\n");
fputs(temp,incbfile);
}
fclose(incfile);
fclose(incbfile);
if(remove(requestfile+1) == 0)
rename(backup,requestfile+1);
else {
warble();
printf("Error changing filelist file\n");
remove(backup);
}
if (files == 0) commentline(line," All files in list have been requested/tried");
}
else { /* Its a standard filename */
fname = strtok(requestfile,"!");
strcpy(password,strtok(NULL," \n"));
results = test_rfile(fname,flfile,FileCount,XferTries);
switch (results) {
case -1: /* File received already */
sprintf(temp," File received on %s",datestr);
commentline(line,temp);
DoNoteMsg(fname);
break;
case -2: /* File already exists in reqfile */
break;
case -3: /* Maximum tries */
break;
case -4:
commentline(line,"Filename has trashed/illegal characters");
break;
case 1: /* File is requested, disabled because magicfilename */
doreqs++;
commentline(line,"MagicFilename, retries disabled");
DoFReqMsg(fname);
break;
case 2: /* File requested, normal */
doreqs++;
results = XferTries;
if (XferTries == -1)
XferTries +=2;
else XferTries++;
if (results == -1) {
results = strlen(line);
line[results-1] = 0; /* Strip linefeed */
strcat(line,"=1\n");
}
else { /* Alter XferTries count */
p = strtok(line,"="); /* Strip out count */
strcpy(temp,p);
sprintf(backup,"=%d\n",XferTries);
strcat(temp,backup);
strcpy(line,temp); /* Redo the line */
}
DoFReqMsg(fname);
break;
}
}
fclose(flfile);
if (doreqs) { /* We did some filerequests, make sure there is a flofile */
flfile = _fsopen(FloFile,"at",SH_DENYWR);
fclose(flfile);
FileReqs += doreqs;
}
else if (reqcreate) /* Did we create a req file? */
remove(ReqFile); /* Don't confuse Binkley then */
}
/* Establish destination node and filespec, check for errors in either */
int setdest(char *input,char *line)
{
char *p;
char *p1;
int code=FALSE;
char nodestr[15];
p1 = (char *) malloc(strlen(input) + 1); /* Make duplicate */
if (p1 == NULL)
aborterror(NOMEM,NULL);
strcpy(p1,input);
DestFile[0] = 0;
p = strtok(p1," ;\t\n"); /* grab the filespec */
if (p != NULL)
strcpy(DestFile,p);
else {
commentline(line,"Invalid filespec!");
free(p1);
return(TRUE);
}
p = strtok(NULL," ;\t\n"); /* Grab the Net/Node string */
strcpy(nodestr,p); /* Copy to temp string */
if (p != NULL) {
if (parsenet(nodestr,&DestZone,&DestNet,&DestNode)) {
commentline(line,"Invalid Net/Node!");
code = TRUE;
}
if (DestNet + DestNode == 0) {
commentline(line,"Blank Net/Node!");
code = TRUE;
}
}
else {
commentline(line,"Invalid Net/Node!");
code = TRUE;
}
free(p1);
return(code);
}
/* Test the given file to see if in the flofile
if not, place it in there. Also test for file existing.
If file does not exist return TRUE, Return -1 if file already
in flow file. */
/* If mode == 1, do file delete after sending
if mode == 2, do file truncate after sending */
int test_sfile(char *fname,FILE *flfile,int mode)
{
struct find_t fsearch;
char tfile[65];
if (!filescan(flfile,fname)) { /* Is it already there? */
/* Okay then first see if file exists */
if(_dos_findfirst(fname,_A_NORMAL,&fsearch)) /* File was not found */
return TRUE;
else {
if (mode == 1) sprintf(tfile,"^%s\n",fname);
else if (mode == 2) sprintf(tfile,"#%s\n",fname);
else sprintf(tfile,"%s\n",fname);
fputs(tfile,flfile); /* Append it */
return FALSE;
}
}
else return -1;
}
/* Test the given file and see if its in the req file, or has been
received. Codes upon return:
-1 == File has been received,
-2 == File already exists in req file,
-3 == Maximum tries
-4 == Invalid filename
1 == File is requested and now req is disabled
2 == File is requested.
filetries is # of times to request file
count is number of times file was already requested */
int test_rfile(char *fname,FILE *reqfile,int filetries,int count)
{
struct find_t fsearch;
char fullname[65];
char *p;
p = fname;
while (*p) { /* check for valid filename */
switch (*p) { /* Check for list of invalid DOS file characters */
case '+':
case '=':
case '/':
case '[':
case ']':
case '"':
case ':':
case ';':
case ',':
case '?':
case '*':
case '\\':
case '<':
case '>':
case '|':
return -4; /* Say it's invalid */
break;
default:
if (*p & 0x80) /* Check high bit ascii */
return -4;
if (*p < ' ')
return -4;
break;
}
p++;
}
sprintf(fullname,"%s\\%s",Inbdir,fname);
if(_dos_findfirst(fullname,_A_NORMAL,&fsearch) == 0) /* File was found */
return -1;
if (filescan(reqfile,fname)) /* Is it already in req file? */
return -2;
if (count >= filetries && filetries != 0)
return -3;
strcpy(fullname,fname);
if (password[0]) {
strcat(fullname," !");
strcat(fullname,password);
}
strcat(fullname,"\n");
fputs(fullname,reqfile);
/* Check to see if it's a magic filename. I.E. it has an extension */
if (strchr(fname,'.') == NULL && filetries != 0)
return 1;
return 2;
}
void DoFSendMsg(char *filename)
{
int msgnum;
FILE *fmsg;
char msgline[81];
char fn[_MAX_FNAME];
char ext[_MAX_EXT];
_splitpath(filename,temp2,temp2,fn,ext);
sprintf(msgline,"File %s is sent to %d/%d",filename,DestNet,DestNode);
logit(msgline,'#');
if (ShowActivity)
sprintf("\r%s\n",msgline);
if (!SysopMail)
return;
ExitCode = 2;
if (FidoMsgSend[0] == 0) { /* Create a new message */
if((msgnum = create_msghdr(DestNode,DestNet,"Frobot Invasion",
MSGPRIVATE | MSGKILL)) == - 1) { /* Some kind of error */
return;
}
sprintf(FidoMsgSend,"%s\\%d.msg",Netmdir,msgnum);
if((fmsg = _fsopen(FidoMsgSend,"ab",SH_DENYWR)) == NULL) {
sprintf(temp,"Error creating fidonet msg %s",FidoMsgSend);
aborterror(BADFILE,temp);
}
fseek(fmsg,0L,SEEK_END);
strcpy(msgline,"\rHello, I am attempting to send you the following file(s): \r\r");
fwrite(msgline,strlen(msgline),1,fmsg);
}
else { /* Re-open it then */
if((fmsg = _fsopen(FidoMsgSend,"ab",SH_DENYWR)) == NULL) {
sprintf(temp,"Error creating fidonet msg %s",FidoMsgSend);
aborterror(BADFILE,temp);
}
}
fseek(fmsg,0L,SEEK_END);
sprintf(msgline,"\t%s%s\r",fn,ext);
fwrite(msgline,strlen(msgline)+1,1,fmsg); /* Include the NULL! */
fclose(fmsg);
}
void DoFReqMsg(char *filename)
{
int msgnum;
FILE *fmsg;
char msgline[81];
sprintf(msgline,"File %s is requested from %d/%d",filename,DestNet,DestNode);
logit(msgline,'#');
if (ShowActivity)
printf("\r%s\n",msgline);
if (!SysopMail)
return;
ExitCode = 2;
if (FidoMsgReq[0] == 0) { /* Create a new message */
if((msgnum = create_msghdr(DestNode,DestNet,"Frobot Invasion",
MSGPRIVATE | MSGKILL)) == - 1) { /* Some kind of error */
return;
}
sprintf(FidoMsgReq,"%s\\%d.msg",Netmdir,msgnum);
if((fmsg = _fsopen(FidoMsgReq,"ab",SH_DENYWR)) == NULL) {
sprintf(temp,"Error creating fidonet msg %s",FidoMsgReq);
aborterror(BADFILE,temp);
}
fseek(fmsg,0L,SEEK_END);
strcpy(msgline,"\rHello, I am attempting to request the following file(s): \r\r");
fwrite(msgline,strlen(msgline),1,fmsg);
}
else { /* Re-open it then */
if((fmsg = _fsopen(FidoMsgReq,"ab",SH_DENYWR)) == NULL) {
sprintf(temp,"Error creating fidonet msg %s",FidoMsgReq);
aborterror(BADFILE,temp);
}
}
fseek(fmsg,0L,SEEK_END);
if (strchr(filename,'.') == NULL)
sprintf(msgline,"\t'%s' <- Magic Filename\r",filename);
else sprintf(msgline,"\t%s\r",filename);
fwrite(msgline,strlen(msgline)+1,1,fmsg); /* Include the NULL! */
fclose(fmsg);
}
void DoNoteMsg(char *filename)
{
int msgnum;
FILE *fmsg;
char msgline[81];
sprintf(msgline,"File %s has been received",filename);
if (ShowActivity)
printf("\r%s\n",msgline);
logit(msgline,'#');
if (!SelfMail)
return;
if (FidoMsgNote[0] == 0) { /* Create a new message */
if((msgnum = create_msghdr(Node,Net,"Files Received",
MSGPRIVATE)) == - 1) { /* Some kind of error */
return;
}
sprintf(FidoMsgNote,"%s\\%d.msg",Netmdir,msgnum);
if((fmsg = _fsopen(FidoMsgNote,"ab",SH_DENYWR)) == NULL) {
sprintf(temp,"Error creating fidonet msg %s",FidoMsgNote);
aborterror(BADFILE,temp);
}
fseek(fmsg,0L,SEEK_END);
strcpy(msgline,"\rHello, you have received the following file(s): \r\r");
fwrite(msgline,strlen(msgline),1,fmsg);
}
else { /* Re-open it then */
if((fmsg = _fsopen(FidoMsgNote,"ab",SH_DENYWR)) == NULL) {
sprintf(temp,"Error creating fidonet msg %s",FidoMsgNote);
aborterror(BADFILE,temp);
}
}
fseek(fmsg,0L,SEEK_END);
sprintf(msgline,"\t%s\r",filename);
fwrite(msgline,strlen(msgline)+1,1,fmsg); /* Include the NULL! */
fclose(fmsg);
}
/* Create a fidonet msg header. Return msg # or -1 if error */
int create_msghdr(int destnode,int destnet,char *subject,unsigned attribute)
{
int x;
FILE *fmsg;
struct _msg *FidoMsgHead;
strcpy(temp2,Netmdir);
strcat(temp2,"\\");
x = find_fidohigh(temp2);
if (x == -1)
return x;
x++;
sprintf(temp2,"%s\\%d.msg",Netmdir,x);
if((fmsg = _fsopen(temp2,"wb",SH_DENYWR)) == NULL) {
sprintf(temp,"Error creating fidonet msg %s",temp2);
aborterror(BADFILE,temp);
}
FidoMsgHead = (struct _msg *) calloc(1,sizeof(struct _msg));
if (FidoMsgHead == NULL)
aborterror(NOMEM,NULL);
/* message header starts out all 0'ed */
sprintf(FidoMsgHead->from,"Frobot Version %2.02f",Version);
strcpy(FidoMsgHead->to,"Sysop");
strcpy(FidoMsgHead->subj,subject);
FidoMsgHead->attr = attribute;
FidoMsgHead->orig_net = (sword) Net;
FidoMsgHead->orig = (sword) Node;
FidoMsgHead->dest = (sword) destnode;
FidoMsgHead->dest_net = (sword) destnet;
FidoMsgHead->date_written.date.da = FidoMsgHead->date_arrived.date.da = ltime->tm_mday;
FidoMsgHead->date_written.date.mo = FidoMsgHead->date_arrived.date.mo = ltime->tm_mon+1;
FidoMsgHead->date_written.date.yr = FidoMsgHead->date_arrived.date.yr = ltime->tm_year-80;
FidoMsgHead->date_written.time.hh = FidoMsgHead->date_arrived.time.hh = ltime->tm_hour;
FidoMsgHead->date_written.time.mm = FidoMsgHead->date_arrived.time.mm = ltime->tm_min;
FidoMsgHead->date_written.time.ss = FidoMsgHead->date_arrived.time.ss = ltime->tm_sec >> 1;
fwrite(FidoMsgHead,sizeof(struct _msg),1,fmsg);
fclose(fmsg);
free(FidoMsgHead);
return(x);
}
/* Return the highest message in an area. -1 if error
msgpath: DOS path for messages (has a trailing \) */
int find_fidohigh(char *msgpath)
{
register int done,highest=0;
int h;
struct find_t c_file;
char apath[66];
sprintf(apath,"%s*.MSG",msgpath);
done = _dos_findfirst(apath,_A_NORMAL,&c_file);
if (done) {
/* Invalid Path and/or empty directory!! */
/* We should test for a sub-directory first before assuming things */
strcpy(apath,msgpath);
h = strlen(apath);
apath[h-1] = 0; /* Strip trailing '\' */
if (apath[h-2] == ':') { /* It's just a drive letter! */
return(0); /* Assume drive letter is ok! */
}
else { /* Its a subdir */
done = _dos_findfirst(apath,_A_SUBDIR,&c_file);
if (done)
return(-1);
else return(0); /* A real subdir, just no files! */
}
}
while (!done && highest < MAXFMSGS){
h = atoi(c_file.name);
if (h>highest) highest=h;
done=_dos_findnext(&c_file);
}
return(highest);
}